﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.NH;

using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Data.Services
{
    /// <summary>
    ///     Provides GetAll() which returns all languages in the application
    ///     and extends Find
    /// </summary>
    public class LanguageDao : BaseDao<Language, int, LanguageFilter>, ILanguageDao
    {
        #region Find
        /// <summary>
        ///     Evaluates all filters and order-by clauses
        ///     and returns sorted list of matching Language entities
        /// </summary>
        /// <param name = "filter">ISearchObject&lt;Language&gt; containing LanguageFilter not-null instance</param>
        /// <returns>List of Language entities matching the searching criteria</returns>
        public override IEnumerable<Language> Find(LanguageFilter filter)
        {
            Criteria.AddCompare(() => filter.IdExact); // Searches for exact ID
            Criteria.AddEqual(() => Entity.LanguageName, filter.LanguageNameSearch);
            Criteria.AddLike(() => filter.LanguageName);

            return GetListByCriteria(filter);
        }
        #endregion Find

        #region ILanguageDao Members
        /// <summary>
        ///     Returns the list of all currently available languages in application
        ///     Encapsulated in try-catch to allow bubble when DB is down.
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<Language> GetAll()
        {
            var langs = Session.CreateCriteria(typeof(Language)).List<Language>();
            foreach (var lang in langs)
            {
                Session.Evict(lang);
            }
            return langs;
        }
        #endregion
    }
}